package uit.nfcmenu.manager;

import java.util.ArrayList;

import uit.nfcmenu.db.adapter.OrderAdapter;
import uit.nfcmenu.db.model.OrderModel;
import uit.nfcmenu.util.CommConstant;
import uit.nfcmenu.util.NetworkHelper;
import uit.nfcmenu.webservice.CommonWS;
import uit.nfcmenu.webservice.MyException;
import uit.nfcmenu.webservice.WSMapping;
import android.content.Context;
import android.util.Log;


public class OrderManager {
	private static String tag = OrderManager.class.getSimpleName();
	
	private static OrderManager instance;
	private Context mContext;
	protected OrderAdapter mOrderAdapter;

	public OrderManager(Context context) {
		mContext = context;
		mOrderAdapter = new OrderAdapter(mContext);
	}

	public static OrderManager getInstance(Context context) {
		if (instance == null) {
			instance = new OrderManager(context);
		}
		return instance;
	}

	public void insertOrderStates(ArrayList<OrderModel> models) {
		for (OrderModel model : models) {
			mOrderAdapter.insertOrderModel(model);
		}
	}

	public void deleteAllOrders() {
		mOrderAdapter.deleteAllOrders();
	}

//	public int changeOrderStatus(OrderModel model) throws MyException {
//		return WSMapping.changeOrderStatus(mContext, model);
//	}
	
	public boolean changeOrderStatus(OrderModel currentOrder) {
		if (currentOrder == null)
			return false;
		String url = CommonWS.Order.createUrlChangeOrderStatus(
				currentOrder.getId(), currentOrder.getStatus(),
				currentOrder.getComment());
		if (NetworkHelper.checkNetworkStatus(mContext)) {
			String respone = null;
			try {
				respone = WSMapping.callWS(url);
			} catch (MyException e1) {
				e1.printStackTrace();
			}
			try {
				if (CommConstant.SIGN_TRUE.equals(respone)) {
					mOrderAdapter.updateOrderModel(currentOrder);
					return true;
				} else {
					return false;
				}
			} catch (Exception e) {
				return false;
			}
		} else {
			CommConstant.savedRequests.add(url);
			return true;
		}
	}
	
	public ArrayList<OrderModel> getOrdersInDB() {
		Log.i(tag, "getOrdersInDB()");
		mOrderAdapter.deleteDummyData();
		
		return mOrderAdapter.getOrdersInDB();
	}
	
//	public ArrayList<OrderModel> getOrdersInDB(String date) {
//		Log.i(tag, "getOrdersInDB() date="+date);
//		return mOrderAdapter.getOrdersInDB(date);
//	}
	
	public void updateOrders() throws MyException {
		// get data from server
		ArrayList<OrderModel> models;
		models = WSMapping.getAllOrderFromServer(mContext);
		
		// update data to db
		OrderAdapter adapter = new OrderAdapter(mContext);
		OrderModel model;
		if (models != null && models.size() > 0) {
			for (int i = 0; i < models.size(); i++) {
				// TODO build #1 => build #2
				// solution build #1, hold still build #2
				model = adapter.loadOrderModel(models.get(i).getId());
				//Log.i("ttnlan", "model="+models.get(i).toString());
				if (model == null) {
					adapter.insertOrderModel(models.get(i));
				} else {
					adapter.updateOrderModel(models.get(i));
				}
			}
		}
	}

	public int postOrders(OrderModel order) throws MyException {
		return WSMapping.postOrder(mContext, order);
	}

}
